<?php


require_once(PATH_tslib.'class.tslib_pibase.php');
require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_view.php');
require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_request.php');

class tx_zlib_controller extends tslib_pibase
{

    /**
     * @var tx_zlib_view
     */
    public $view;

    /**
     * @var tx_zlib_snippet
     */
    public $snippet;

    /**
     * @var tx_zlib_request
     */
    private $request;

    /**
     * @var string
     */
    public $content;

    /**
     * @var string
     */
    public $js;

    /**
     * @var string
     */
    public $css;

    /**
     * @var boolean
     */
    private $wrapInBaseClass = true;

    /**
     * @var boolean
     */
    private $loadLL = true;

    /**
     * @var boolean
     */
    private $initPIflexForm = true;

    /**
     * @param string $content
     * @param array $conf
     * @return string
     */
	public function main($content, $conf)
	{
	    $this->content = $content;
		$this->conf = $conf;

        $this->initRequest();
		$this->initView();
		$this->init();

		$this->pi_setPiVarDefaults();

		if($this->loadLL) {
		  $this->pi_loadLL();
		}

		if($this->initPIflexForm) {
            $this->pi_initPIflexForm();
		}

		// Action
		$actionMethod = $this->getActionMethodName();
        $methods = get_class_methods($this);
        if(in_array('preAction', $methods)) {
            $this->preAction();
        }
		if(in_array($actionMethod, $methods)) {
            $this->$actionMethod();
		} else {
            $this->errorAction();
		}
        if(in_array('postAction', $methods)) {
            $this->postAction();
        }

		if(!empty($this->js)) {
            //$GLOBALS['TSFE']->setJS($this->prefixId, $this->js);
            $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] .= TSpagegen::inline2TempFile($this->js, 'js');
		}

		if(!empty($this->css)) {
            //$GLOBALS['TSFE']->setCSS($this->prefixId, $this->css);
            $GLOBALS['TSFE']->additionalHeaderData[$this->prefixId] .= TSpagegen::inline2TempFile($this->css, 'css');
		}

		if($this->wrapInBaseClass) {
            return $this->pi_wrapInBaseClass($this->content);
		} else {
            return $this->content;
		}
	}

	/**
	 * @return void
	 */
	public function init()
	{}

    /**
     * @return void
     */
    public function initRequest()
    {
        $this->request = new tx_zlib_request($this->prefixId);
    }

	/**
	 * @return void
	 */
	public function initView()
	{
	    $extId = $this->getExtId();
        $extKey = $this->getExtKey();

    	$this->view = new tx_zlib_view();
        $this->view->addPath(t3lib_extMgm::extPath('zlib')  . 'views/scripts');
    	$this->view->addPath(t3lib_extMgm::extPath($extKey) . "views/scripts");
    	$this->view->addPath(t3lib_extMgm::extPath($extKey) . "{$extId}/views/scripts");

		$viewHelper = new tx_zlib_viewhelper($this);
        $viewHelper->addPath(t3lib_extMgm::extPath('zlib')  . 'views/helpers', 'tx_zlib_viewhelper_');
    	$viewHelper->addPath(t3lib_extMgm::extPath($extKey) . 'views/helpers', "tx_{$extKey}_viewhelper_");
    	$viewHelper->addPath(t3lib_extMgm::extPath($extKey) . "{$extId}/views/helpers", "tx_{$extKey}_{$extId}_viewhelper_");

    	$this->view->setHelper($viewHelper);
	}

	/**
	 * @return void
	 */
	public function initSnippet()
	{
        require_once(t3lib_extMgm::extPath('zlib') . 'class.tx_zlib_snippet.php');

	    $extId = $this->getExtId();
        $extKey = $this->getExtKey();

    	$this->snippet = new tx_zlib_snippet();
        $this->snippet->addPath(t3lib_extMgm::extPath('zlib') . 'scripts/snippets');
    	$this->snippet->addPath(t3lib_extMgm::extPath($extKey) . "scripts/snippets");
    	$this->snippet->addPath(t3lib_extMgm::extPath($extKey) . "{$extId}/scripts/snippets");
    }

	/**
	 * @param string $viewScript
     * @param string $extension
	 * @return string
	 */
	public function render($viewScript, $extension = 'phtml')
	{
	    $content = $this->view->render($viewScript, $extension);
	    $this->content .= $content;
	    return $content;
	}

	/**
	 * @param string $viewScript
     * @param string $extension
     * @return string
	 */
	public function renderJs($viewScript, $extension = 'js')
	{
	    $prefix = 'js/';
        if(strpos($viewScript, $prefix) === false) {
            $viewScript = $prefix . $viewScript;
        }
        $js = $this->view->render($viewScript, $extension);
        $this->js .= $js;
        return $js;
	}

	/**
	 * @param string $viewScript
     * @param string $extension
     * @return string
	 */
	public function renderCss($viewScript, $extension = 'css')
	{
	    $prefix = 'css/';
        if(strpos($viewScript, $prefix) === false) {
            $viewScript = $prefix . $viewScript;
        }
        $css = $this->view->render($viewScript, $extension);
        $this->css .= $css;
        return $css;
	}

	/**
	 * @return string
	 */
	public function getExtId()
	{
        $parts = explode('/', $this->scriptRelPath);
        return isset($parts[0]) ? trim($parts[0]) : '';
	}

	/**
	 * @return string
	 */
	public function getExtKey()
	{
	    return trim($this->extKey);
	}

    /**
     * @return string
     */
    public function getAction()
    {
        $action = $this->getParam('action', '');
        return empty($action) ? 'default' : $action;
    }

    /**
     * @return string
     */
    public function getActionMethodName()
    {
        return $this->getAction() . 'Action';
    }

    /**
     * return void
     */
    public function errorAction()
    {
        $this->view->actionName = $this->getActionMethodName();
        $this->view->className = get_class($this);
        return $this->render('error');
    }

    /**
     * @param string $fieldName
     * @return string
     */
    public function getExtConfValue($fieldName)
	{
        return $this->view->getHelper()->extConf($fieldName);
	}

    /**
     *
     * @param string $fieldName
     * @param string $sheet
     * @param string $lang
     * @param string $value
     * @return string
     */
    public function getFlexformValue($fieldName, $sheet='sDEF', $lang='lDEF', $value='vDEF')
    {
        return $this->view->getHelper()->flexform($fieldName, $sheet, $lang, $value);
    }

    /**
     * @param boolean $flag
     */
    public function setWrapInBaseClass($flag)
    {
        $this->wrapInBaseClass = (bool)$flag;
    }

    /**
     * @param boolean $flag
     */
    public function setLoadLL($flag)
    {
        $this->loadLL = (bool)$flag;
    }

    /**
     * @param boolean $flag
     */
    public function setInitPIflexForm($flag)
    {
        $this->initPIflexForm = (bool)$flag;
    }

    /**
     * Gets a parameter from piVars.  If the
     * parameter does not exist, NULL will be returned.
     *
     * If the parameter does not exist and $default is set, then
     * $default will be returned instead of NULL.
     *
     * @param string $paramName
     * @param mixed $default
     * @return mixed
     */
    protected function getParam($paramName, $default = null)
    {
        $value = isset($this->piVars[$paramName]) ? $this->piVars[$paramName] : null;
        if ((null == $value) && (null !== $default)) {
            $value = $default;
        }
        return $value;
    }

    /**
     * Return all piVar parameters
     *
     * @return array
     */
    protected function getParams()
    {
        return $this->piVars;
    }

    /**
     * Set a parameter in the piVars.
     *
     * @param string $paramName
     * @param mixed $value
     * @return void
     */
    protected function setParam($paramName, $value)
    {
        $this->piVars[$paramName] = $value;
    }

    /**
     * @return integer
     */
    public function getLanguageId()
    {
        return intval($GLOBALS['TSFE']->sys_language_uid);
    }

    /**
     * @return integer
     */
    public function getPageId()
    {
        return intval($GLOBALS['TSFE']->page['uid']);
    }

    /**
     * @return integer
     */
    public function getContentId()
    {
        return intval($this->cObj->data['uid']);
    }

	/**
	 * Link to the current page.
	 * Returns an URL to the CURRENT page, but with $urlParameters set as extra parameters for the page.
	 *
	 * @param	string		The content string
	 * @param	array		Array with URL parameters as key/value pairs. They will be "imploded" and added to the list of parameters defined in the plugins TypoScript property "parent.addParams" plus $this->pi_moreParams.
	 * @param	boolean		If $cache is set (0/1), the page is asked to be cached by a &cHash value (unless the current plugin using this class is a USER_INT). Otherwise the no_cache-parameter will be a part of the link.
	 * @param	integer		Alternative page ID for the link. (By default this function links to the SAME page!)
	 * @return	string		The input string
	 * @see pi_linkTP_keepPIvars(), tslib_cObj::typoLink()
	 */
	public function pi_linkTP_url($urlParameters=array(), $cache=0, $altPageId=0)
	{
	    $cache = (bool)$cache;
        return $this->view->getHelper()->url()->thisPage($urlParameters, $cache, $altPageId);
	}

    /**
     * @return tx_zlib_request
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * @return tx_zlib_viewhelper
     */
    public function getHelper()
    {
        return $this->view->getHelper();
    }

}


if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_controller.php'])	{
	include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/zlib/class.tx_zlib_controller.php']);
}
